<?php defined('SYSPATH') or die('No direct script access.');
/**
 * Controller_Upload
 *
 * Steuert den Ablauf des /upload Strangs, Koordinniert die Speicherung der Bilder
 *
 * @copyright m.reschke
 * @author m.reschke
 * @version $Id$
 */

class Controller_Upload extends Controller_Template_Website
{
    /**
     * Verarbeitet den Bild Upload, übergibt Werte an View und an Flash Message
     *
     */
    public function action_index()
    {
        if ($this->request->method() == Request::POST) {

           $resizeWidth = intval($this->request->post('resizeWidth'));
            if (!empty($resizeWidth) && is_int($resizeWidth)) {
                $resizeWidth = $resizeWidth;
            } else {
                $resizeWidth = NULL;
            }

            if (isset($_FILES['imageupload'])) {
                $image = new Model_Image();
                $filename = $image->saveImage($_FILES['imageupload'], $resizeWidth);
                if (!$filename['image']) {
                    $errorMessage = 'Error! Make sure that your image is a JPG / PNG / GIF file.';
                    Message::set( Message::ERROR, $errorMessage );
                } else {
                    $imageUrl = URL::base(true) . $filename['image'];
                    $showUrl = str_replace('image', 'show', $imageUrl);
                    Message::set( Message::SUCCESS, $showUrl );
                }
            }
        }

        $this->template->content = View::factory('welcome/index');
        $this->redirect('welcome/index');
    }

    /**
     * Verarbeitet den Remote Bild Upload, übergibt Werte an View und an Flash Message
     *
     */
    public function action_remote()
    {
        if ($this->request->method() == Request::POST) {

            $post = $this->request->post(); // alle POST Werte holen

            $validation = Validation::factory($post);
            $validation
                ->rule('remoteImgPath', 'not_empty')
                ->rule('remoteImgPath', 'url');

            if ($validation->check()) {

                if (isset($post['resizeWidth'])) {
                    $resizeWidth = intval($post['resizeWidth']);
                    if (!empty($resizeWidth) && is_int($resizeWidth)) {
                        $resizeWidth = $resizeWidth;
                    } else {
                        $resizeWidth = NULL;
                    }
                } else {
                    $resizeWidth = NULL;
                }

                if (isset($post['remoteImgPath'])) {
                    $image = new Model_Image();
                    $filename = $image->saveRemoteImage($post['remoteImgPath'], $resizeWidth);
                    if (!$filename['image']) {
                        $errorMessage = 'Error! Make sure that your image is a JPG / PNG / GIF file.';
                        Message::set( Message::ERROR, $errorMessage );
                    } else {
                        $imageUrl = URL::base(true) . $filename['image'];
                        $showUrl = str_replace('image', 'show', $imageUrl);
                        Message::set( Message::SUCCESS, $showUrl );
                    }
                }

            } else {
                Message::set( Message::ERROR, $validation->errors('uploadMessages') );
                $this->redirect('welcome/remote');
            }

        }

        $this->template->content = View::factory('welcome/remote');
        $this->redirect('welcome/remote');
    }

    /**
     * Verarbeitet den Multi Bild Upload, übergibt Werte an View und an Flash Message
     *
     */
    public function action_multi()
    {
        if ($this->request->method() == Request::POST) {

            $resizeWidth = intval($this->request->post('resizeWidth'));
            if (!empty($resizeWidth) && is_int($resizeWidth)) {
                $resizeWidth = $resizeWidth;
            } else {
                $resizeWidth = NULL;
            }

            $errorMessage = 'Error! Make sure that your image is a JPG / PNG / GIF file.';

            if (isset($_FILES['multiUploadHtml'])) {

                $image = new Model_Image();
                $filenames = $image->saveMultiImage($_FILES['multiUploadHtml'], $resizeWidth);

                if ($filenames) {

                    foreach ($filenames as $filename) {

                        if (!$filename['image']) {
                            Message::set( Message::ERROR, $errorMessage );
                        } else {
                            $imageUrl = URL::base(true) . $filename['image'];
                            $showUrl = str_replace('image', 'show', $imageUrl);
                            $showUrls[] = $showUrl;
                        }

                    }

                    if (isset($showUrls)) {
                        Message::set( Message::SUCCESS, $showUrls );
                    } else {
                        Message::set( Message::ERROR, $errorMessage );
                    }

                } else {
                    Message::set( Message::ERROR, $errorMessage );
                }


            }
        }

        $this->template->content = View::factory('welcome/multi');
        $this->redirect('welcome/multi');
    }

}
